<template>
  <div class='xtx-goods-page' v-if="detail.categories" >
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <!-- <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem to="/">手机</XtxBreadItem>
        <XtxBreadItem to="/">华为</XtxBreadItem>
        <XtxBreadItem to="/">p30</XtxBreadItem> -->
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem >{{detail.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem >{{detail.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem >{{detail.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
         <!-- 新增结构 -->
    <!-- 图片预览区 -->
    <div class="media">
      <GoodsImage :imageList="detail.mainPictures" />
      <GoodsSales />
    </div>
    <!-- 商品信息区 -->
    <div class="spec">
      <GoodName />
                <!-- sku选择=>商品属性选择 -->
      <GoodsSku :goods="detail" @change="selSku"/>
      <!-- 数量加减 -->
      <XtxNumbox :max="detail.inventory" v-model="buyNum" />

      <!-- 按钮 -->

      <Button @click="insertCart" style="margin-top:20px;" type="primary" >加入购物车</Button>
          </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs" v-if="detail.details">
           <img v-imgLazy="url"
              v-for="url in detail.details.pictures"
              :key="url"
            />

          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods.js'
import { provide, ref } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import { useStore } from 'vuex'
import Msg from '@/components/Message/index.js'
// import store from '@/store'
const useDetailData = () => {
  const route = useRoute()
  const detail = ref({})
  const buyNum = ref(10)
  const getDetail = async () => {
    const res = await findGoods(route.params.id)
    detail.value = res.result
    console.log(res)
  }
  provide('detail', detail)
  getDetail()
  return { buyNum, detail }
}
const useSkuSel = (detail) => {
  const currSku = ref(null)
  const selSku = (skuInfo) => {
    console.log(skuInfo)
    if (skuInfo.skuId) {
      detail.value.oldPrice = skuInfo.oldPrice
      detail.value.price = skuInfo.price
      detail.value.inventory = skuInfo.inventory
      currSku.value = skuInfo
    } else {
      currSku.value = null
    }
  }
  return { selSku, currSku }
}
const useAddCart = (currSku, detail, buyNum) => {
  // 加入购物车
  const store = useStore()
  const insertCart = async () => {
    // 没有完整的sku数据 直接return
    if (!currSku.value) {
      return Msg({ text: '请选择商品规格' })
    }
    // 库存为0
    if (currSku.value.inventory === 0) {
      return Msg({ text: '库存不足' })
    }
    // store.dispatch('cart/')
    // 执行加入购物车
    // 当前要存入购物车的商品数据
    const currGood = {
      id: detail.value.id,
      name: detail.value.name,
      picture: detail.value.mainPictures[0],
      skuId: currSku.value.skuId, // sku属性ID（唯一）
      price: currSku.value.oldPrice, // 价格
      nowPrice: currSku.value.price,
      attrsText: currSku.value.specsText,
      stock: currSku.value.inventory, // 库存
      selected: true, // 是否在购物车选中
      isEffective: true, // 是否是有效商品
      count: buyNum.value // 购买数量
    }
    console.log(currGood)
    try {
      store.dispatch('cart/addCartAction', currGood)
      Msg({ type: 'success', text: '加购成功' })
      // Msg({ type: 'success', text: '加购成功' })
    } catch (error) {
      console.log(error)
    }
  }
  return { insertCart }
}
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsImage,
    GoodsSales,
    GoodName
  },
  setup () {
    const { buyNum, detail } = useDetailData()
    const { selSku, currSku } = useSkuSel(detail)
    const { insertCart } = useAddCart(currSku, detail, buyNum)

    return { detail, selSku, buyNum, insertCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
    .goods-info {
      min-height: 600px;
      background: #fff;
      display: flex;
      // 新增样式
      .media {
        width: 580px;
        height: 600px;
        padding: 30px 50px;
      }
      .spec {
        flex: 1;
        padding: 30px 30px 30px 0;
      }
    }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
